package com.nstar.base.container
{
	import mx.containers.Tile;
    import mx.core.EdgeMetrics;
    import mx.containers.TileDirection;
    import mx.core.UIComponent;
    import mx.core.mx_internal;
    
    use namespace mx_internal;

    public class Flow extends Tile {
        public function Flow() {
            super();
        }
        
        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
            
            // The measure function isn't called if the width and height of
            // the FlowContainer are hard-coded. In that case, we compute the cellWidth
            // and cellHeight now.
            if (isNaN(cellWidth) || isNaN(cellHeight)) {
                findCellSize();
            }
            
            var vm:EdgeMetrics = viewMetricsAndPadding;
            
            var paddingLeft:Number = getStyle("paddingLeft");
            var paddingTop:Number = getStyle("paddingTop");
            
            var horizontalGap:Number = getStyle("horizontalGap");
            var verticalGap:Number = getStyle("verticalGap");
            
            var xPos:Number = paddingLeft;
            var yPos:Number = paddingTop;
            var i:int;
            var child:UIComponent;
            
            if (direction == TileDirection.HORIZONTAL) {
                var xEnd:Number = Math.ceil(unscaledWidth) - vm.right;
                
                for (i = 0; i < numChildren; i++) {
                    child = UIComponent(getChildAt(i));
            
                    if (!child.includeInLayout) {
                        continue;
                    }
            
                    // Start a new row?
                    if (xPos + child.measuredWidth > xEnd) {
                        // Only if we have not just started one...
                        if (xPos != paddingLeft) {
                            yPos += (cellHeight + verticalGap);
                            xPos = paddingLeft;
                        }
                    }
                    child.move(xPos, yPos);
            
                    xPos += (child.measuredWidth + horizontalGap);
                }
            } else {
                var yEnd:Number = Math.ceil(unscaledHeight) - vm.bottom;
            
                for (i = 0; i < numChildren; i++) {
                    child = UIComponent(getChildAt(i));
            
                    if (!child.includeInLayout) {
                        continue;
                    }
            
                    // Start a new column?
                    if (yPos + child.measuredHeight > yEnd) {
                        // Only if we have not just started one...
                        if (yPos != paddingTop) {
                            xPos += (cellWidth + horizontalGap);
                            yPos = paddingTop;
                        }
                    }
                    child.move(xPos, yPos);
                    
                    yPos += (child.measuredHeight + verticalGap);
                }
            }
            
            // Clear the cached cell size, because if a child's size changes
            // it will be invalid. These cached values are only used to
            // avoid recalculating in updateDisplayList() the same values
            // that were just calculated in measure().
            // They should not persist across invalidation/validation cycles.
            cellWidth = NaN;
            cellHeight = NaN;
        }
    }
}